<?php
	session_start();

	require_once 'photo.php';
	require_once 'configu.php';
	require_once 'database.php';
	$operation = $_POST["operation"];
	
	if ($operation=='avt'){
		$idfoto = $_SESSION["komenidf"];
		$username = $_SESSION["username"];
		photo::setAsAvatar($username,$idfoto);
		//header("Location: ".$CFG->url."detail.php?id=".$idfoto);
		require 'viewer/header.php';
	}
	else if ($operation=='addcomment'){	
		$hasil = photo::addComment($_SESSION["komenuname"],$_SESSION["komenidf"],$_SESSION["username"], $_POST["cominput"]);
		//header("Location: ".$CFG->url."detail.php?un=".$_SESSION["komenuname"]."&id=".$_SESSION["komenidf"]);
	}
	else if ($operation=='delcomment'){		
		photo::delComment($_POST["komenname"],$_POST["idfoto"],$_POST["idcomment"]);
	}
	else if ($operation=='editdetail'){
		photo::changeDescription($_SESSION["username"], $_SESSION["komenidf"], $_POST["idesc"]);
		photo::changeHakAkses($_SESSION["username"], $_SESSION["komenidf"], $_POST["hakakses"]);
		photo::changeHakKomen($_SESSION["username"], $_SESSION["komenidf"], $_POST["hakkomen"]);
		photo::changeCategory($_SESSION["username"], $_SESSION["komenidf"], $_POST["kategori"]);
		
		//header("Location: ".$CFG->url."detail.php?id=".$_SESSION["komenidf"]);			
	}
	else if ($operation=='delphoto'){
		photo::deletePhoto($_SESSION["komenuname"], $_SESSION["komenidf"]);
		//header("Location: ".$CFG->url."fts.php");
	}
	else if ($operation=='like'){
		//photo::doLike($_SESSION["username"], $_SESSION["komenidf"]);
		photo::doNilai( $_SESSION["komenuname"], $_SESSION["komenidf"], $_SESSION["username"], 1);
		//header("Location: ".$CFG->url."detail.php?id=".$_SESSION["komenidf"]);			
	}
	else if ($operation=='dislike'){
		//photo::doDislike($_SESSION["username"], $_SESSION["komenidf"]);
		photo::doNilai( $_SESSION["komenuname"], $_SESSION["komenidf"], $_SESSION["username"], -1);
		//header("Location: ".$CFG->url."detail.php?id=".$_SESSION["komenidf"]);			
	}
	else if ($operation=='tagphoto'){
		$stringTag = $_POST["taglist"];
		photo::changeTagged($_SESSION["username"], $_SESSION["komenidf"], str_replace(" ", "", $stringTag));
		//header("Location: ".$CFG->url."detail.php?id=".$_SESSION["komenidf"]);
	}
	else if ($operation=='upload'){
		require_once 'uploader.php';
		
		$username = $_SESSION["username"];
		$filefoto = $_FILES["foto"]["name"];
		$pathfoto = 'users/'.$username.'/'.$filefoto;
		$desc	= $_POST["idesc"];
		$tglAmbil = $_POST["tglambil"];
		
		if ($tglAmbil == ''){$tglAmbil = getdate();}
		
		$categori = $_POST["kategori"];
		$keyword  = $_POST["ikeyword"];
		
		if($_POST["hakkomen"] == 'every') $hakkomen = 0;
		else if ($_POST["hakkomen"] == 'follower') $hakkomen = 1;
		else if ($_POST["hakkomen"] == 'me') $hakkomen = 2;
		else $hakkomen = 0;
		
		if ($_POST["hakakses"] == 'every') $hakakses = 0;
		else if ($_POST["hakakses"] == 'follower') $hakakses = 1;
		else if ($_POST["hakakses"] == 'me') $hakakses = 2;
		else $hakakses = 0;
		
		while (photo::isPhotoExist($username,$filefoto))
		{
			$filefoto = '_'.$filefoto;
			$pathfoto = 'users/'.$username.'/'.$filefoto;
		}
		photo::addPhoto($username,$filefoto,$tglAmbil,$desc,$categori,$hakakses,$hakkomen);
			
		uploader::uploadFoto($pathfoto);	
		header("Location: ".$CFG->url."fts.php");	
	}
	else if($operation=='editprofile'){
		if ($_POST[password] != NULL)
			database::query("UPDATE user SET Password = '". md5($_POST[password])."', Nama = '".$_POST[nama]."', Email = '".$_POST[email]."', TanggalLahir = '".$_POST[lahir]."' WHERE Username = '".$_SESSION['username']."'");
		else
			database::query("UPDATE user SET Nama = '".$_POST[nama]."', Email = '".$_POST[email]."', TanggalLahir = '".$_POST[lahir]."' WHERE Username = '".$_SESSION['username']."'");
		
		header("Location: ".$CFG->url."profile.php?".$_SESSION[username]);
	}

?>